namespace jQube.Web.UI.Controls
{
    using System;
    using System.Collections;
    using System.Collections.Generic;

    public class AccordionPaneCollection : IList, IEnumerable<AccordionPane>
    {
        private Accordion parent;

        internal AccordionPaneCollection(Accordion parent)
        {
            this.parent = parent;
        }

        internal Accordion Parent
        {
            get
            {
                return this.parent;
            }
        }

        public int Add(AccordionPane pane)
        {
            this.parent.Controls.Add(pane);

            return this.IndexOf(pane);
        }

        public void Clear()
        {
            this.parent.Controls.Clear();
        }

        public bool Contains(AccordionPane pane)
        {
            return this.parent.Controls.Contains(pane);
        }

        public int IndexOf(AccordionPane pane)
        {
            return this.parent.Controls.IndexOf(pane);
        }

        public void Insert(int index, AccordionPane pane)
        {
            this.parent.Controls.AddAt(index, pane);
        }

        public void Remove(AccordionPane pane)
        {
            if (this.Contains(pane))
                this.parent.Controls.Remove(pane);
        }

        public void RemoveAt(int index)
        {
            this.parent.Controls.RemoveAt(index);
        }

        public AccordionPane this[int index]
        {
            get
            {
                return this.parent.Controls[index] as AccordionPane;
            }
        }

        public int Count
        {
            get
            {
                return this.parent.Controls.Count;
            }
        }

        public void CopyTo(Array array, int index)
        {
            this.parent.Controls.CopyTo(array, index);
        }

        public IEnumerator<AccordionPane> GetEnumerator()
        {
            return new AccordionPaneEnumerator(this);
        }

        #region IList Members

        int IList.Add(object value)
        {
            if (value is AccordionPane)
                return this.Add(value as AccordionPane);
            else
                return -1;
        }

        void IList.Clear()
        {
            this.Clear();
        }

        bool IList.Contains(object value)
        {
            if (value is AccordionPane)
                return this.Contains(value as AccordionPane);
            else
                return false;
        }

        int IList.IndexOf(object value)
        {
            if (value is AccordionPane)
                return this.IndexOf(value as AccordionPane);
            else
                return -1;
        }

        void IList.Insert(int index, object value)
        {
            if (value is AccordionPane)
                this.Insert(index, value as AccordionPane);
        }

        bool IList.IsFixedSize
        {
            get { return false; }
        }

        bool IList.IsReadOnly
        {
            get { return false; }
        }

        void IList.Remove(object value)
        {
            if (value is AccordionPane)
                this.Remove(value as AccordionPane);
        }

        void IList.RemoveAt(int index)
        {
            this.RemoveAt(index);
        }

        object IList.this[int index]
        {
            get
            {
                return this[index];
            }
            set
            {
                throw new InvalidOperationException("The collection is read-olny");
            }
        }

        #endregion

        #region ICollection Members

        void ICollection.CopyTo(Array array, int index)
        {
            this.CopyTo(array, index);
        }

        int ICollection.Count
        {
            get { return this.Count; }
        }

        bool ICollection.IsSynchronized
        {
            get { return false; }
        }

        object ICollection.SyncRoot
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion
    }
}
